package com.ml.carManager;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;

import androidx.multidex.MultiDex;

import com.hjq.toast.ToastUtils;
import com.ml.carManager.ui.crash.HintCrashActivity;
import custom.base.utils.GlideUtils;

import custom.base.crash.Cockroach;
import custom.base.data.Global;
import custom.base.data.Mode;
import custom.base.log.GlobeLog;
import custom.base.log.MLog;

/**
 * Created by luozi on 2015/12/29.
 */
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        /**
         * 设置运行模式
         * */
        Global.getInstance().initLaunchMode(getApplicationContext());
        Global.getInstance().setLaunchMode(Mode.Launch.DEBUG, getApplicationContext());
        /**
         * 控制模式
         * */
        Global.getInstance().setControlMode(Mode.Control.RELEASE);
        // 初始化 Toast 框架
        ToastUtils.init(this);
        GlideUtils.getInstance().init(getApplicationContext());


        initCrash();
    }

    /**
     * 初始化崩溃
     */
    private void initCrash() {
        Cockroach.install(new Cockroach.ExceptionHandler() {
            // handlerException内部建议手动try{  你的异常处理逻辑  }catch(Throwable e){ } ，以防handlerException内部再次抛出异常，导致循环调用handlerException
            @Override
            public void handlerException(final Thread thread, final Throwable throwable) {
                try {
                    throwableCount = 0;
                    final String res = thread + "\n" + getThrowable(throwable).toString();

                    String userID = "";

                    MLog.e(res);
                    GlobeLog.getIntance().addLog(getApplicationContext(), "错误日志", res);
                    throwable.printStackTrace();
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent(getApplicationContext(), HintCrashActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("content", res);
                            getApplicationContext().startActivity(intent);
                        }
                    });
                } catch (Throwable e) {
                    MLog.e("捕获crash时的异常 -> " + e.toString());
                }

            }
        });
    }

    int throwableCount = 0;

    /**
     * 递归错误文本
     */
    private StringBuilder getThrowable(Throwable throwable) {
        StringBuilder builder = new StringBuilder(throwable.toString());
        if (throwableCount >= 100)//超过100行以上则不递归了
            return builder.append("异常错误哦过长到达100行时自动停止");
        throwableCount++;
        StackTraceElement[] elements = throwable.getStackTrace();
        if (elements != null) {
            for (int i = 0; i < elements.length; i++) {
                builder.append("\nat " + elements[i].toString());
            }
        }
        Throwable t = throwable.getCause();
        if (t != null) {
            return getThrowable(t);
        }
        return builder;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
